package com.toubutu.audio.advice;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.toubutu.common.BusinessException;
import com.toubutu.common.Result;
import com.toubutu.common.StatusCodeEnum;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionControllerAdvice {


    @ExceptionHandler(BusinessException.class)
    public Result<String> handleNotFoundException(BusinessException e){
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class})
    public Result<String> handleRequestMethodNotSupported(HttpRequestMethodNotSupportedException exception){
        return Result.fail("请求方式不合法!");
    }

    @ExceptionHandler(TokenExpiredException.class)
    public Result<String> handleTokenExpiredException(TokenExpiredException exception){
        return Result.fail(StatusCodeEnum.TOKEN_EXPIRED);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException argumentNotValidException){
        String defaultMessage = argumentNotValidException.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return Result.fail(StatusCodeEnum.USER_HANDLE.getCode(),defaultMessage);
    }
}
